﻿// 4. Write a program that finds the maximal sequence of equal elements in an array.
//		Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} -> {2, 2, 2}.

using System;

class FindMaxSequenceOfEqualArrayElements
{
    static void Main()
    {
        // get the length of the array
        Console.Write("Enter length of the array : ");
        string length = Console.ReadLine();
        int lengthOfArray;
        while (!int.TryParse(length, out lengthOfArray))
        {
            Console.Write("Invalid input. Enter again length for the array : ");
            length = Console.ReadLine();
        }

        if (lengthOfArray <= 0)
        {
            Console.WriteLine("Inavlid input for array's length. Start the program again.");
        }
        else
        {
            int[] array = new int[lengthOfArray];

            for (int index = 0; index < lengthOfArray; index++)
            {
                // get the element of the array
                Console.Write("Enter element of the array : ");
                string element = Console.ReadLine();
                int elementOfArray;
                while (!int.TryParse(element, out elementOfArray))
                {
                    Console.Write("Invalid input. Enter element again : ");
                    element = Console.ReadLine();
                }
                array[index] = elementOfArray;
            }

            int sequenceLength = 1;
            int startIndex = 0;
            int bestSequenceLength = 1;
            int bestStartIndex = 0;
            for (int index = 1; index < lengthOfArray; index++)
            {
                if (array[index] == array[index - 1])
                {
                    sequenceLength++;
                    if (sequenceLength > bestSequenceLength)
                    {
                        bestSequenceLength = sequenceLength;
                        bestStartIndex = startIndex;
                    }
                }
                else
                {
                    startIndex = index;
                    sequenceLength = 1;
                }
            }
            Console.WriteLine("Best lenght sequence is : {0}", bestSequenceLength);

            string resultString = "";
            for (int i = 0; i < bestSequenceLength; i++)
            {

                if (i == bestSequenceLength -1)
                {
                    resultString = resultString + array[bestStartIndex];
                }
                else
                {
                    resultString = resultString + array[bestStartIndex] + ", ";
                }
            }
            Console.WriteLine("And the sequence is : {" + resultString + "}");
        }
    }
}
